package team.mutong.taxi.utils;

import java.io.ByteArrayInputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import team.mutong.taxi.domain.GPSPoint;

/**
 * @author TomWang
 * @email wanghuochen@outlook.com
 * @date 2016年5月9日 下午9:17:05
 * @version 1.0
 */
public class ChangeType {
	/**
	 * 將gps坐標轉換成二維數組的形式
	 * 
	 * @param src
	 *            gps坐標字符串
	 * @return 二維數組
	 */
	public static double[][] changeToList(String src) {
		String srctemp = src.substring(2, src.length() - 2);
		String[] seclist = srctemp.split("\\],\\[");
		double[][] result = new double[seclist.length][2];
		for (int i = 0; i < seclist.length; i++) {
			String temp = seclist[i];
			String[] templist = temp.split(",");
			for (int j = 0; j < 2; j++) {
				result[i][j] = Double.valueOf(templist[j]);
			}
		}
		return result;
	}

	/**
	 * 將gps坐標轉換成list，GPSPoint對象的形式
	 * 
	 * @param src
	 *            gps坐標字符串
	 * @return GPSPoint的list
	 */
	public static List<GPSPoint> changeToObject(String src) {
		String srctemp = src.substring(2, src.length() - 2);
		String[] seclist = srctemp.split("\\],\\[");
		List<GPSPoint> result = new ArrayList<>();
		for (int i = 0; i < seclist.length; i++) {
			String temp = seclist[i];
			String[] templist = temp.split(",");
			GPSPoint point = new GPSPoint(Double.valueOf(templist[0]),
					Double.valueOf(templist[1]));
			result.add(point);
		}
		return result;
	}
	/**
	 * 将坐标的blob类型转成String
	 * @param SetBlob
	 * @return
	 * @author TomWang
	 */
	public static String blobChangetoString(Blob SetBlob) {
		String result = "";
		try{
			ByteArrayInputStream msgContent = (ByteArrayInputStream) SetBlob.getBinaryStream();
			byte[] byte_data = new byte[msgContent.available()];
			result = new String(byte_data);
		} catch(SQLException e){
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 将坐标的beyt[]类型转成String
	 * @param SetBlob
	 * @return
	 * @author TomWang
	 */
	public static String blobChangetoString(byte[] SetBlob) {
		String result = "";
		result = new String(SetBlob);
		return result;
	}
}
